package cn.lili.modules.promotion.manage;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.promotion.entity.dos.BlindboxCount;
import cn.lili.modules.promotion.entity.dos.BlindboxRule;
import cn.lili.modules.promotion.mapper.BlindboxCountMapper;
import cn.lili.modules.promotion.service.BlindboxCountService;
import cn.lili.modules.promotion.service.BlindboxPrizeRecordService;
import cn.lili.modules.promotion.service.BlindboxRelService;
import cn.lili.modules.promotion.service.BlindboxRuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiangqian
 * @Date 2023/10/4 18:45
 */
@Component
public class BlindboxPrizePaperManage {

    @Resource
    private BlindboxRelService blindboxRelService;
    @Resource
    private BlindboxRuleService blindboxRuleService;
    @Resource
    private BlindboxCountMapper blindboxCountMapper;

    /**
     * 查询盲盒规则
     *
     * @param goodsId 商品ID
     * @return 盲盒规则
     */
    public BlindboxRule getBlindboxRule(String goodsId) {
        //查询盲盒规则
        String ruleId = blindboxRelService.getRuleIdByGoodsId(goodsId);
        if (StringUtils.isEmpty(ruleId)) {
            throw new ServiceException("商品未参加盲盒活动");
        }
        BlindboxRule blindboxRule = blindboxRuleService.getById(ruleId);
        if (blindboxRule == null) {
            throw new ServiceException("商品未参加盲盒活动");
        }
        return blindboxRule;
    }

    /**
     * 查询盲盒子统计信息-已抽奖卷
     *
     * @param goodsId    商品ID
     * @param ruleBuyNum 购买次数（购买多少次算一次奖卷）
     */
    public Integer getPrizeNumber(String goodsId, Integer ruleBuyNum) {
        //检索条件
        QueryWrapper<BlindboxCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
        queryWrapper.eq("delete_flag", false);
        List<BlindboxCount> list = blindboxCountMapper.selectList(queryWrapper);

        if (list == null || list.size() < 1) {
            return 0;
        }
        int total = list.stream()
                .mapToInt(value -> value.getBuyNum() / ruleBuyNum)
                .reduce(0, Integer::sum);

        return total;
    }


    /**
     * 查询盲盒子统计信息-我的购买次数
     *
     * @param goodsId    商品ID
     * @param userId 用户ID
     */
    public Integer getMyPrizeNumber(String goodsId,String userId) {
        //检索条件
        QueryWrapper<BlindboxCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("delete_flag", false);
        List<BlindboxCount> list = blindboxCountMapper.selectList(queryWrapper);

        if(list != null){
            Integer totalCount = list.stream()
                    .mapToInt(BlindboxCount::getBuyNum)
                    .sum();
            return totalCount;
        }

        return 0;
    }
}
